//
//  UITableViewCell+HDChatReplyCell.m
//  app
//
//  Created by D on 15/6/11.
//  Copyright (c) 2015年 hdaren. All rights reserved.
//

#import "HDChatReplyCell.h"

static const CGFloat kMargin = 24.0f;
static const CGFloat kFaceH = 88.0f;

@implementation HDChatReplyCell

-(void)cellWithModel:(HDChatReplyModel *)model
{
    if (model.textHeight < 20) {
        self.Hight = kFaceH/2;
    }else{
        self.Hight = model.textHeight+20;
    }
    
    if (model.textWight < 50) {
        self.Wight = 60.0f;
    }else{
        self.Wight = model.textWight;
    }
    
    UIImage * BGimage = [UIImage imageNamed:@"chat_you.png"];
    CGFloat top = UP(70);
    CGFloat bottom = UP(16);
    CGFloat left = UP(40);
    CGFloat right = UP(16);
    UIEdgeInsets insets = UIEdgeInsetsMake(top, left, bottom, right);
    BGimage = [BGimage resizableImageWithCapInsets:insets resizingMode:UIImageResizingModeStretch];
    
    //创建头像
   self.face = [[UIImageView alloc]initWithFrame:CGRectOf([self.ubo ux:kMargin fy:UB_1_2 uy:-kFaceH/2], [self.ubo uw:kFaceH uh:kFaceH])];
        if ([[SDImageCache sharedImageCache] imageFromDiskCacheForKey:model.uid]) {
            
            [self.face setImage:[[SDImageCache sharedImageCache] imageFromDiskCacheForKey:model.uid]];
        }else
        {
            [self.face sd_setImageWithURL:[NSURL URLWithString:model.face] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                
                NSData * data = UIImageJPEGRepresentation(image,1.0);
                [[SDImageCache sharedImageCache] storeImage:image recalculateFromImage:YES imageData:data forKey:model.uid toDisk:YES];
            }];
        }
    self.face.layer.masksToBounds = YES;
    self.face.layer.cornerRadius = UF(10);
    [self.contentView addSubview:self.face];
    
    
    //创建对话框
    self.backImage = [[UIImageView alloc]initWithFrame:CGRectOf([self.face.ufo fx:1 ux:0 fy:UB_1_2 uy:-kFaceH/2], [self.ubo uw:self.Wight*2+40 uh:self.Hight*2])];
    [self.backImage setImage:BGimage];
    [self.contentView addSubview:self.backImage];
    
    
    self.content = [[HDMsgLable alloc]initFrame:CGRectOf([self.face.ufo fx:1 ux:30 fy:UB_1_2 uy:-kFaceH/2], [self.ubo uw:self.Wight*2 uh:self.Hight*2])];
    self.content.layer.masksToBounds = YES;
    self.content.layer.cornerRadius = UF(10);
    self.content.text = model.content;
    self.content.lineBreakMode = NSLineBreakByCharWrapping;
    self.content.numberOfLines = 0;
    self.content.backgroundColor = [UIColor clearColor];
    self.content.font = JKAppGlobleFontContentText;
    [self.contentView addSubview:self.content];
    
    
    //创建透明button
    self.jumpBtn = [[HDReplyButton alloc]initWithFrame:CGRectOf([self.face.ufo fx:1 ux:0 fy:UB_1_2 uy:-kFaceH/2], [self.ubo uw:self.Wight*2+40 uh:self.Hight*2])];
    self.jumpBtn.tag = model.msgid.integerValue;
    self.jumpBtn.forumd = model.forum;
    self.jumpBtn.card = model.card;
    [self.jumpBtn addTarget:self action:@selector(jumpCardContent:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:self.jumpBtn];
    
    HDAppDelegate *appDelegate = (HDAppDelegate *)[UIApplication sharedApplication].delegate;
    self.frame = CGRectMake(0, 0, appDelegate.window.frame.size.width, UF(self.Hight*2+56));
    
    
}

-(void)jumpCardContent:(HDReplyButton*)sender
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(jumpCardWithButton:)]) {
        [self.delegate jumpCardWithButton:sender];
    }
}

@end















